package marinagosson.ti.trabalho.servlets;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import marinagosson.ti.trabalho.bean.Cliente;
import marinagosson.ti.trabalho.database.DAOFactory;

/**
 * Servlet implementation class CadastroServlet
 */
@WebServlet("/CadastroServlet")
public class CadastroServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public CadastroServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		System.out.println("doGet - Cadastrar no Banco");

		Cliente cliente = new Cliente();
		RequestDispatcher rd = null;

		cliente.setNome(request.getParameter("nome"));
		cliente.setEmail(request.getParameter("email"));
		cliente.setSenha(request.getParameter("senha"));
		cliente.setCpf(request.getParameter("cpf"));
		cliente.setRg(request.getParameter("rg"));
		cliente.setRua(request.getParameter("rua"));
		cliente.setNumero(request.getParameter("numero"));
		cliente.setComplemento(request.getParameter("complemento"));
		cliente.setCep(request.getParameter("cep"));
		cliente.setEstado(request.getParameter("estado"));
		cliente.setCidade(request.getParameter("cidade"));

		try {
			DAOFactory.getClienteDao().create(cliente);
		} catch (Exception e) {
			System.out.println("Erro ao cadastrar o usuario.");
			rd = request.getRequestDispatcher("/login.html");
			rd.include(request, response);
			e.printStackTrace();
		} finally {
			rd = request.getRequestDispatcher("/index.html");
			rd.forward(request, response);
		}

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}